import re
import xlwt
# file=open("sex.txt",mode="w")#开启一个txt文档
txt = open(r"C:\Users\patrickjane\Desktop\chromehistoryview\111.txt", "r", encoding='utf-16').read()  #txt编码格式不是utf-8而是utf-16

result=""
test_text = re.findall("..............+html", txt)#取出每行含有pass的文本
result = result +'\n'.join(test_text)#换行输出
# 将字符串写入excel文件，方法一
# excel = open("111.xlsx", "w").write(result)
# xlsx.save('111.xlsx')  # 保存
# print("finish")
wk = xlwt.Workbook(encoding="utf-8")
sheet_test = wk.add_sheet("test")
sheet_test = open("111.xlsx", "w").write(result)

# file.write(result)#写入
# print(result)
# print(type(result))

"""
import xlwt
from pypinyin import lazy_pinyin
import pandas as pd

# 读入EXCEL文件
filepath = "../test_word/lyj.xlsx"
ex = pd.read_excel(filepath)
result = ""
for i in range(ex.shape[0]):
    zh_word = (ex.iloc[i, 0])
    test_list = lazy_pinyin(zh_word)
    # result = result + ''.join(test_list) + ','#不换行
    result = result + ''.join(test_list) + '\n'  # 换行

    # 将字符串写入txt文件
    txt = open("../test/test0401.txt", "w").write(result)

    # 将字符串写入excel文件，方法一
    excel = open("../test/test0401.xlsx", "w").write(result)

    # 将字符串写入excel文件，方法二
    x = 1  # 在第二行开始写
    y = 0  # 在第一列开始写
    xls = xlwt.Workbook()
    sheet = xls.add_sheet('sheet', cell_overwrite_ok=True)  # 生成excel的方法，声明excel

    for z in result.split("\n"):
        sheet.write(0, 0, '拼音')  # 在第一行第一列单元格写"拼音"
        sheet.write(x, y, z)  # x代表行，y代表列
        x += 1
    y += 1
    y = 0
    xls.save('E://untitled1/test/Excel_test0401.xlsx')  # 保存
    print("finish")
"""

"""import xlsxwriter

workbook = xlsxwriter.Workbook('sex1.xlsx') # 建立文件

worksheet = workbook.add_worksheet() # 建立sheet， 可以work.add_worksheet('employee')来指定sheet名，但中文名会报UnicodeDecodeErro的错误
title = [U'名称',U'副标题']     #表格title
worksheet.write_row('A1',title)  #title 写入Excel
for i in range(1,100):
    num0 = bytes(i+1)
    num = bytes(i)
    row = 'A' + num0
    data = [u'学生'+num,num,]
    worksheet.write_row(row, data)
    i+=1

workbook.close()
"""













